package com.youuvs.service.impl;

import com.youuvs.domain.MovieTicket;
import com.youuvs.domain.UserTicket;
import com.youuvs.mapper.MovieTicketMapper;
import com.youuvs.service.IMovieTicketService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.youuvs.service.IUserTicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhaoby
 * @since 2022-08-29
 */
@Service
public class MovieTicketServiceImpl extends ServiceImpl<MovieTicketMapper, MovieTicket> implements IMovieTicketService {
    @Autowired
    private IUserTicketService userTicketService;
    @Autowired
    private MovieTicketMapper movieTicketMapper;

    @Override
    public MovieTicket getTicketByUserId(Long userId) {
        //先拿到ticketid
        UserTicket userTicket = userTicketService.selectById(userId);

        return movieTicketMapper.selectById(userTicket.getMoveTicketId());

    }

    @Override
    public Long maxId() {
        return movieTicketMapper.maxId();
    }

    @Override
    public Long minId() {
        return movieTicketMapper.minId();
    }

    @Override
    public Long randomTicket() {
        //拿到随机的ticketId
        int min = movieTicketMapper.minId().intValue();
        int max = movieTicketMapper.maxId().intValue();
        //long l = Long.valueOf("123").longValue();
        long id = Long.valueOf(getRandom(min, max)).longValue();
        return id;
    }


    //获取随机整数的方法
    public static String getRandom(int min, int max) {
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return String.valueOf(s);
    }
}